package com.example.musiccommunity.util;

import com.example.musiccommunity.bean.Record;
import com.example.musiccommunity.bean.Song;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MusicRecommender {
    private ArrayList<Song> allSongs; // 所有歌曲库
    private ArrayList<Record> recordArrayList; // 所有记录

    public MusicRecommender(ArrayList<Song> songs,ArrayList<Record> recordArrayList ) {
        this.allSongs = songs;
        this.recordArrayList = recordArrayList;
    }

    private Map<String, Integer> getUserRecordStats( ) {
        Map<String, Integer> stats = new HashMap<>();
        // 统计音乐风格   统计歌手
        for (Record r:recordArrayList
             ) {
            stats.put("style_" + r.getStyle(), stats.getOrDefault("style_" + r.getStyle(), 0) + 1);
            stats.put("artist_" + r.getSinger(), stats.getOrDefault("artist_" + r.getSinger(), 0) + 1);
            stats.put("id_" + r.getId(), stats.getOrDefault("id_" + r.getSinger(), 0) + 1);
        }
        return stats;
    }


    public ArrayList<Song> recommendSongs(  int numRecommendations) {
        if(recordArrayList.size()<5) {//  听歌记录小于5，返回随机推荐
            return (ArrayList<Song>) getRandomRecommendations(numRecommendations);
        }

        Map<String, Integer> userStats = getUserRecordStats();
        // 如果没有听歌记录，返回随机推荐
        if(userStats.isEmpty()) {
            return (ArrayList<Song>) getRandomRecommendations(numRecommendations);
        }
        // 计算每首歌的推荐分数
        Map<Song, Integer> songScores = new HashMap<>();
        for(Song song : allSongs) {
            int score = 0;
            // 风格匹配加分
            String styleKey = "style_" + song.getStyle();
            if(userStats.containsKey(styleKey)) {
                score += userStats.get(styleKey) * 3; // 风格权重更高
            }
            // 歌手匹配加分
            String artistKey = "artist_" + song.getArtist();
            if(userStats.containsKey(artistKey)) {
                score += userStats.get(artistKey) * 2;
            }
            songScores.put(song, score);
        }
        // 按分数排序并获取前N首
        return (ArrayList<Song>) songScores.entrySet().stream()
                .sorted(Map.Entry.<Song, Integer>comparingByValue().reversed())
                .limit(numRecommendations)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }


    public ArrayList<Song> recommendSongsNew(  int numRecommendations) {
//        if(recordArrayList.size()<5) {//  听歌记录小于5，返回随机推荐
//            return (ArrayList<Song>) getRandomRecommendations(numRecommendations);
//        }
//
        Map<String, Integer> userStats = getUserRecordStats();
//        // 如果没有听歌记录，返回随机推荐
//        if(userStats.isEmpty()) {
//            return (ArrayList<Song>) getRandomRecommendations(numRecommendations);
//        }
        // 计算每首歌的推荐分数
        Map<Song, Integer> songScores = new HashMap<>();
        for(Song song : allSongs) {
            int score = 0;
            // 风格匹配加分
//            String styleKey = "style_" + song.getStyle();
//            if(userStats.containsKey(styleKey)) {
//                score += userStats.get(styleKey) * 3; // 风格权重更高
//            }
//            // 歌手匹配加分
//            String artistKey = "artist_" + song.getArtist();
//            if(userStats.containsKey(artistKey)) {
//                score += userStats.get(artistKey) * 2;
//            }


            String idKey = "id_" + song.getRawResId();
            if(userStats.containsKey(idKey)) {
                score += userStats.get(idKey) * 2;
            }
            songScores.put(song, score);

        }
        // 按分数排序并获取前N首
        return (ArrayList<Song>) songScores.entrySet().stream()
                .sorted(Map.Entry.<Song, Integer>comparingByValue().reversed())
                .limit(numRecommendations)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }


    /**
     * 随机推荐歌曲
     */
    public List<Song> getRandomRecommendationsNew(int num) {
        System.out.println("随机了");
        Collections.shuffle(allSongs);
        int nowNum = 0;
        if(num>=10){
            nowNum=0;
        }else{
            nowNum=10-num;
        }


        return allSongs.stream().limit(Math.abs(nowNum)).collect(Collectors.toList());
    }

    /**
     * 随机推荐歌曲
     */
    private List<Song> getRandomRecommendations(int num) {
        System.out.println("随机了");
        Collections.shuffle(allSongs);
        return allSongs.stream().limit(num).collect(Collectors.toList());
    }
}
